package com.gwcloud.leetcode.interview100;

import java.util.Stack;

/** 
* @ClassName   : Leetcode0150 
* @author      : ganwei 
* @date        : 2020年5月28日 
* @Description : 150. 逆波兰表达式求值 
*/
public class Leetcode0150 {
    /**
     * -解题思路，
     */
	public int evalRPN(String[] tokens) {
		Stack<Integer> s = new Stack<Integer>();
		for(int i = 0; i < tokens.length; i++) {
			if(tokens[i].equals("+")) {
				int y = s.pop();
				int x = s.pop();
				s.push(x+y);
			}else if(tokens[i].equals("-")) {
				int y = s.pop();
				int x = s.pop();
				s.push(x-y);
			}else if(tokens[i].equals("*")) {
				int y = s.pop();
				int x = s.pop();
				s.push(x*y);
			}else if(tokens[i].equals("/")) {
				int y = s.pop();
				int x = s.pop();
				s.push(x/y);
			}else {
				s.push(Integer.valueOf(tokens[i]));
			}
		}
		
		return s.pop();
    }
    
    /**
     * -主函数
     */
    public static void main(String[] args) {
    	Leetcode0150 l0150 = new Leetcode0150();
    	String[] tokens = {"4","13","5","/","+"};
    	System.out.println(l0150.evalRPN(tokens));
    	//System.out.println(13/5);
    }
}
